package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"strings"

	"github.com/gin-gonic/gin"
)

// PageSysConfig 获取所有配置
func PageSysConfig(c *gin.Context) {

	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " (`name` like " + keyword + " or `key` like " + keyword + ")"
	}
	list, page, total, err := services.NewSysConfigService(utils.MysqlClient).PageSysConfigList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetUser 获取配置
func GetSysConfig(c *gin.Context) {

	var dataRequest struct {
		Keyword string `json:"keyword" form:"keyword"`
	}

	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.Keyword != "" {
		keyword := "%" + dataRequest.Keyword + "%"
		where += " (name like " + keyword + " or key like " + keyword + ")"
	}
	data, err := services.NewSysConfigService(utils.MysqlClient).GetSysConfig(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// CreateUser 创建配置
func CreateSysConfig(c *gin.Context) {

	/**
	Name  string `json:"name"`
	Key   string `json:"key"`
	Value string `json:"value"`
	AppID int    `json:"app_id"`
	*/
	var createRequest struct {
		Name  string `json:"name" form:"name"`
		Key   string `json:"key" form:"key"`
		Value string `json:"value" form:"value"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysConfigCreate{
		Name:  createRequest.Name,
		Key:   createRequest.Key,
		Value: createRequest.Value,
	}
	data, err := services.NewSysConfigService(utils.MysqlClient).CreateSysConfig(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	services.NewSysConfigService(utils.MysqlClient).DelCahceConfig(c)
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新配置
func UpdateSysConfig(c *gin.Context) {
	var createRequest struct {
		ID    uint   `json:"id" form:"id"`
		Name  string `json:"name" form:"name"`
		Key   string `json:"key" form:"key"`
		Value string `json:"value" form:"value"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysConfigUpdate{
		ID:    createRequest.ID,
		Name:  createRequest.Name,
		Key:   createRequest.Key,
		Value: createRequest.Value,
	}
	data, err := services.NewSysConfigService(utils.MysqlClient).UpdateSysConfig(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	services.NewSysConfigService(utils.MysqlClient).DelCahceConfig(c)
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除配置
func DeleteSysConfig(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewSysConfigService(utils.MysqlClient).DeleteSysConfig(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewSysConfigService(utils.MysqlClient).DeleteSysConfigs(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	services.NewSysConfigService(utils.MysqlClient).DelCahceConfig(c)
	// ... 这里添加删除配置的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete data"})
}
